# Copyright (C) 2002-2006, 2009 The Written Word, Inc.
#
# This program is free software; you can redistribute it and/or modify
# it under the terms of the GNU General Public License as published by
# the Free Software Foundation; either version 2, or (at your option)
# any later version.
#
# This program is distributed in the hope that it will be useful, but
# WITHOUT ANY WARRANTY; without even the implied warranty of
# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
# General Public License for more details.
#
# You should have received a copy of the GNU General Public License
# along with this program; if not, write to the Free Software Foundation,
# Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA.
#
# $Id: patch.py 748 2010-11-02 11:25:38Z gary $

import os
import sys

import misclib
import prog

from prog.archive import Archive, pipe_two_commands

class Archive (Archive):
  def unpack (self, dry_run, verbose, callback, dest = None,
              filelist = None, list_files = False):
    if not dry_run:
      self.verify_md5_sig ()

    _gpatch_path = misclib.find_path_prog ('gpatch', self.environ)
    if not _gpatch_path:
      raise prog.ProgProgramNotFound ('gpatch')

    if not dry_run:
      if not os.path.exists (self.build_prefix):
        raise prog.ProgArchiveError ('build prefix "' + build_prefix +
                                     '" does not exist. maybe you \
specified the wrong build directory in the build-name element?')

    arg = self.arg
    if not arg:
      arg = '-p0'

    if verbose:
      callback ('msg', '$ ' + _gpatch_path + ' ' + arg +
                ' -i ' + os.path.normpath (self.path) + '\n', 2)
    if not dry_run:
      if os.spawnve (os.P_WAIT, _gpatch_path, [_gpatch_path, arg,
                                               '-i', self.path],
                     self.environ):
        raise prog.ProgArchiveError ('error using gpatch to patch source')

class ArchiveGzip (Archive):
  def unpack (self, dry_run, verbose, callback, dest = None,
              filelist = None, list_files = False):
    if not dry_run:
      self.verify_md5_sig ()

    _gpatch_path = misclib.find_path_prog ('gpatch', self.environ)
    if not _gpatch_path:
      raise prog.ProgProgramNotFound ('gpatch')
    _gzip_path = misclib.find_path_prog ('gzip', self.environ)
    if not _gzip_path:
      raise prog.ProgProgramNotFound ('gzip')

    if not dry_run:
      if not os.path.exists (self.build_prefix):
        prog.ProgArchiveError ('build prefix "' + self.build_prefix +
                               '" does not exist. maybe you \
specified the wrong build directory in the build-name element?')
        raise ProgUnpackError

    arg = self.arg
    if not arg:
      arg = '-p0'

    if verbose:
      callback ('msg', '$ ' + _gzip_path + ' -dc ' +
                        os.path.normpath (self.path) + ' | ' +
                _gpatch_path + ' ' + arg + '\n', 2)
    if not dry_run:
      if pipe_two_commands (_gzip_path, [ '-dc', self.path ],
                            _gpatch_path, [ arg ], self.environ):
        prog.ProgArchiveError ('error using gzip | gpatch to unpack source')
